<script>
var e = require('@babel/runtime/helpers/typeof');
export default {
  data() {
    return {
      nTop: '',
      nHeight: '',
      nTH: '',
      pages: '',
    };
  },
  onLaunch: function () {
    // this.environment = uni.getSystemInfoSync().uniPlatform
    var e = uni.getUpdateManager();
    if (e) {
      e.onCheckForUpdate(function (e) {
      });
      e.onUpdateReady(function () {
        uni.showModal({
          title: '更新提示',
          content: '新版本已经准备好，是否重启应用？',
          success: function (n) {
            if (n.confirm) {
              e.applyUpdate();
            }
          }
        });
      });
      e.onUpdateFailed(function () {
      });
    }
    this.globalData.get_config('_ZEAI');
    this.globalData.get_openid();
  },
  globalData: {
    aid: 0,
    U: [],
    hn_par: {},
    index_par: {},
    checkboxMaxNum: 8,
    WWW: 'https://www.youfenglove.com/',
    // WWW: 'https://www.miaodaokj.com/',
    _ZEAI: {},
    _VIP: {},
    environment: uni.getSystemInfoSync().uniPlatform, // mp-weixin 微信小程序 app App webH5
    get_config: function (e) {
      var that = this;
      return new Promise(function (t, o) {
        var i = uni.getStorageSync('COOK_ZEAI');
        if (that.empty(i.color) || that.empty(i.siteName) || that.empty(i.ifps)) {
          that.ajax({
            url: 'get_config.php',
            data: {
              f: e,
              ver: 10
            },
            success: function (o) {
              if ('_ZEAI' === e) {
                that._ZEAI = o;
                uni.setStorageSync('COOK_ZEAI', {
                  ifps: o.ifps,
                  siteName: o.siteName,
                  color: o.color,
                  loveb: o.loveb,
                  cache_str: o.cache_str,
                  tabbar: o.tabbar
                });
              } else {
                if ('_VIP' == e) {
                  that._VIP = o;
                }
              }
              t(o);
            }
          });
        } else {
          that._ZEAI = i;
          t(i);
        }
      });
    },

    get_openid: function () {
      var that = this;
      return new Promise(function (n, t) {
        var o = uni.getStorageSync('COOK_U');
        var i = false;
        uni.getSystemInfo({
          success: function (e) {
            if ('singlePage' == e.mode) {
              i = true;
              if ('function' == typeof n) {
                n(o.openid);
              }
              return true;
            }
          }
        });
        if (i) {
          if ('function' == typeof n) {
            n(o.openid);
          }
          return true;
        }
        if (that.empty(o.openid) || that.empty(o)) {
          if (that.environment === 'mp-weixin') {
            uni.login({
              success: function (t) {
                that.ajax({
                  url: 'get_openid.php',
                  data: {
                    code: t.code
                  },
                  success: function (e) {
                    uni.setStorageSync('COOK_U', {
                      openid: e.U.openid,
                      unionid: e.U.unionid,
                      session_key: e.U.session_key
                    });
                    if ('function' == typeof n) {
                      n(e.U.openid);
                    }
                  }
                });
              }
            });
          }
        } else {
          if ('function' == typeof n) {
            n(o.openid);
          }
        }
      });
    },

    chklogin: function (e) {
      const that = this;
      return new Promise(function (e, t) {
        if (that.environment === 'mp-weixin') {
          var o = uni.getStorageSync('COOK_U');
          if (!that.empty(o.openid) && (1 === o.flag || -2 === o.flag)) {
            e(true);
            return false;
          }
          var i = false;
          uni.getSystemInfo({
            success: function (n) {
              if ('singlePage' == n.mode) {
                i = true;
                uni.setStorageSync('COOK_U', {
                  id: 0,
                  openid: '',
                  unionid: '',
                  flag: 0,
                  kind: 1
                });
                if ('function' == typeof e) {
                  e(o.openid);
                }
                return true;
              }
            }
          });
          if (i) {
            if ('function' == typeof e) {
              e(o.openid);
            }
            return true;
          }
          uni.showLoading({
            title: '玩命加载中',
            mask: true
          });
          uni.hideLoading();
          uni.login({
            success: function (t) {
              that.ajax({
                url: 'login.php',
                data: {
                  code: t.code,
                  submitok: 'chklogin'
                },
                success: function (n) {
                  switch (n.flag) {
                    case 'reged':
                      switch (
                          (uni.setStorageSync('COOK_U', {
                            id: n.U.id,
                            openid: n.U.openid,
                            unionid: n.U.unionid,
                            flag: n.U.flag,
                            kind: n.U.kind
                          }),
                              parseInt(n.U.flag))
                          ) {
                        case 1:
                        case -2:
                          e(true);
                          break;
                        case -1:
                          uni.showModal({
                            content: '您的帐号ID：' + n.U.id + '已被锁定或注销，请联系管理员',
                            confirmText: '朕知道了',
                            showCancel: false,
                            success: function (e) {
                              uni.exitMiniProgram({
                                success: function (e) {
                                }
                              });
                            }
                          });
                          break;
                        case 0:
                          uni.showModal({
                            content: '您已注册成功，请等待我们审核',
                            confirmText: '朕知道了',
                            showCancel: false,
                            success: function (e) {
                            }
                          });
                          break;
                        case 2:
                          uni.navigateTo({
                            url: '/pagesA/reg/reg'
                          });
                      }
                      break;
                    case 'noreg':
                      e(false);
                      break;
                    default:
                      uni.showModal({
                        content: n.msg,
                        confirmText: '朕知道了',
                        showCancel: false,
                        success: function (e) {
                        }
                      });
                  }
                  uni.hideLoading();
                }
              });
            }
          });
        } else {
          return true;
        }
      });
    },

    empty: function (e) {
      return '' === e || 'NAN' === e || 0 === e || null == e || 'null' === e || 'undefined' === e || undefined === e;
    },

    ifmob: function (e) {
      return !!/^1[3,4,5,6,7,8,9]\d{9}$/.test(e);
    },

    ifweixin: function (e) {
      return !(!/^[a-zA-Z]([-_a-zA-Z0-9]{5,19})+$/.test(e) && !this.ifmob(e));
    },

    ajax: function (e) {
      var that = this;
      var urls = '';
      // 微信小程序环境检测
      if (typeof window !== 'undefined' &&
          typeof document !== 'undefined') {
        // H5环境检测
        urls = `/api/wxxcx/${e.url}`
      } else {
        urls = that.WWW + '/api/wxxcx/' + e.url
      }
      uni.request({
        url: urls,
        method: e.method || 'GET',
        data: e.data,
        header: {
          'content-type': 'application/json',
        },
        success: function (t) {
          t.statusCode.toString().startsWith('2')
              ? e.success && e.success(that.jsoneval(t.data))
              : uni.showToast({
                title: '抱歉，请求出错',
                icon: 'none',
                mask: true
              });
        },
        fail: function (e) {
          uni.showToast({
            title: '抱歉，刚刚网络开了个小差',
            icon: 'none',
            mask: true
          });
          uni.hideLoading();
        }
      });
    },

    pay: function (e) {
      var n = uni.getStorageSync('COOK_U');
      var t = e.data;
      var that = this;
      t.openid = n.openid;
      this.ajax({
        url: 'pay.php',
        data: t,
        success: function (n) {
          if (1 == n.flag) {
            var t = n.paydata;
            uni.requestPayment({
              timeStamp: t.timeStamp,
              nonceStr: t.nonceStr,
              package: t.package,
              signType: 'MD5',
              paySign: t.paySign,
              success: function (t) {
                var o = t.errMsg;
                'requestPayment:ok' == (o = o.replace(/\s+/g, ''))
                    ? 'function' == typeof e.success
                        ? e.success(n)
                        : (uni.showToast({
                          title: '支付成功！',
                          icon: 'success',
                          mask: true,
                          duration: 2000
                        }),
                            setTimeout(function () {
                              if (getCurrentPages().length >= 2) {
                                uni.navigateBack({})
                              } else {
                                uni.switchTab({
                                  url: '/pages/my/my',
                                })
                              }
                            }, 2000))
                    : 'function' == typeof e.fail
                        ? e.fail(n)
                        : uni.showToast({
                          title: '支付失败！',
                          icon: 'none',
                          mask: true,
                          duration: 2000
                        });
              },
              fail: function (n) {
                'function' == typeof e.fail ? e.fail(n) : uni.navigateBack();
              },
              complete: function (e) {
              }
            });
          } else {
            'function' == typeof e.fail
                ? e.fail(n)
                : uni.showModal({
                  content: n.msg,
                  confirmColor: that._ZEAI.color,
                  confirmText: '朕知道了',
                  showCancel: false,
                  success: function (e) {
                  }
                });
          }
        }
      });
    },

    jsoneval: function (n) {
      return 'object' != e(n) ? ((n = n.replace(/\ufeff/g, '')), JSON.parse(n)) : n;
    },

    get_navigationBarTopHeight: function (e) {
      uni.getSystemInfo({
        success: function (n) {
          let i = 44;
          var t = n.statusBarHeight;
          /*#ifdef MP-WEIXIN */
          var o = uni.getMenuButtonBoundingClientRect();
          i = 2 * (o.top - t) + o.height;
          /* #endif */

          var a = getCurrentPages();
          e.setData({
            nTop: t,
            nHeight: i,
            nTH: t + i,
            pages: a
          });
        }
      });
    },

    get_pages: function (e) {
      var n = getCurrentPages();
      e.setData({
        pages: n
      });
    },

    ifint: function (e) {
      return !!/^[0-9]*[1-9][0-9]*$/.test(e);
    },

    getPhoneNumber: function (e) {
      var that = this;
      var t = e.e;
      var o = that.ifint(e.tguid) ? e.tguid : 0;
      var i = that.ifint(e.admid) ? e.admid : 0;
      uni.login({
        success: function (a) {
          uni.request({
            url: that.WWW + '/api/wxxcx/get_openid.php',
            data: {
              code: a.code
            },
            success: function (a) {
              a = that.jsoneval(a.data);
              uni.getStorageSync('COOK_U');
              uni.request({
                url: that.WWW + '/api/wxxcx/get_mob.php',
                data: {
                  session_key: encodeURIComponent(a.U.session_key),
                  encryptedData: encodeURIComponent(t.detail.encryptedData),
                  iv: encodeURIComponent(t.detail.iv),
                  openid: a.U.openid,
                  submitok: e.submitok,
                  tguid: o,
                  admid: i
                },
                success: function (t) {
                  t = that.jsoneval(t.data);

                  'navigateTo' == e.urlkind
                      ? uni.navigateTo({
                        url: e.url
                      })
                      : uni.reLaunch({
                        url: e.url
                      });
                },
                fail: function (e) {
                  uni.showModal({
                    content: e,
                    showCancel: false
                  });
                }
              });
            }
          });
        }
      });
    },

    openSetting: function (e, n) {
      uni.requestSubscribeMessage({
        tmplIds: e,
        success: function (e) {
          if ('function' == typeof n) {
            n(e);
          }
        },
        fail: function (e) {
        }
      });
    }
  }
};
</script>
<style>
@import './app.css';
</style>
